<template>
	<view :class="classes" @longpress="payAudio" @click="stopAudio"  >
		<image class="icon" :src="src"></image>
		<text class="duration">{{ data.duration }}</text>
	</view>
</template>

<script>
	const innerAudioContext = uni.createInnerAudioContext(); //音频
	
	export default {
		props: {
			data: {
				type: Object,
				default: () => {}
			},
			right: Boolean
		},
		data() {
			return {
				src: '/static/img/chat/audio1.png'
			}
		},
		computed: {
			classes() {
				return ['chat-sound-wrap', { 'chat-sound-right-wrap': this.data.right }]
			}
		},
		methods: {
			payAudio() {
				this.src = '/static/img/chat/audio.gif'
				innerAudioContext.src = this.data.content
				
				this.$nextTick(function() {
					innerAudioContext.play();
				});
			},
			stopAudio() {
				
				this.$nextTick(function() {
					innerAudioContext.stop();
					this.src = '/static/img/chat/audio1.png'
					
				});
			},
		}
	}
</script>

<style lang="scss">
	.chat-sound-wrap {
		height: 40px;
		background-color: white;
		width: 80px;
		border-radius: 8px;
		justify-content: flex-start;
		flex-direction: row;
		align-items: center;
		.duration {
			font-size: 14px;
			margin-left: 6px;
		}
		.icon {
			width: 18px;
			height: 18px;
			margin-left: 10px;
		}
		&.chat-sound-right-wrap {
			background-color: #1E89FF;
			.icon {
				width: 18px;
				height: 18px;
				margin-right: 10px;
			}
		}
	}
</style>
